import { http } from "@/utils/http";

export type Role = {
  id: number;
  name: string;
  description?: string;
  status: number;
  created_at: string;
  updated_at: string;
  deleted_at?: string;
};

export type RoleResponse = {
  success: boolean;
  code: number;
  message: string;
  data: Role;
};

export type RoleListResponse = {
  success: boolean;
  code: number;
  message: string;
  data: Role[];
  pagination: {
    page: number;
    page_size: number;
    total: number;
    total_page: number;
  };
};

export type PaginationParams = {
  page?: number;
  page_size?: number;
};

/** 创建角色 */
export const createRole = (data: Partial<Role>) => {
  return http.request<RoleResponse>("post", "/role", { data });
};

/** 获取角色列表 */
export const getRoles = (params?: PaginationParams) => {
  return http.request<RoleListResponse>("get", "/role", { params });
};

/** 获取角色详情 */
export const getRoleById = (id: number) => {
  return http.request<RoleResponse>("get", `/role/${id}`);
};

/** 更新角色 */
export const updateRole = (id: number, data: Partial<Role>) => {
  return http.request<RoleResponse>("put", `/role/${id}`, { data });
};

/** 删除角色 */
export const deleteRole = (id: number) => {
  return http.request("delete", `/role/${id}`);
};

/** 获取管理员的角色 */
export const getRolesByAdminId = (id: number) => {
  return http.request<RoleListResponse>("get", `/admin/${id}/roles`);
};

/** 为管理员分配角色 */
export const assignRoleToAdmin = (admin_id: number, role_id: number) => {
  return http.request("post", "/role/assign", { params: { admin_id, role_id } });
};

/** 从管理员移除角色 */
export const removeRoleFromAdmin = (admin_id: number, role_id: number) => {
  return http.request("delete", "/role/remove", { params: { admin_id, role_id } });
};

/** 获取拥有指定角色的管理员 */
export const getAdminsByRoleId = (id: number, params?: PaginationParams) => {
  return http.request<AdminListResponse>("get", `/role/${id}/admins`, { params });
};